#include <stdio.h>

#include "wifi.h"
#include "network.h"
#include "error_check.h"

#if 0
void app_main(void)
{
    wifi_connect_config("Yangu", "5201314ygyu", NULL);
#if 0
    // UDP 客户端测试
    int port = 8000;
    char ip[] = "10.7.189.99";

    int udpSock =  UDP_Init(ip, &port, UDPCLIMODE);
    int i = 0;
    char bufData[128] = "";
    char buf[] = ":张鑫是个小可爱";

    //error_check_start();

    while(i++ < 100){
        char bufi[4] = "";
        sprintf(bufi, "%d", i);
        strcat(bufData, bufi);
        strcat(bufData, buf);

        if(sendto(udpSock, bufData, strlen(bufData)+1, 0, (SA*)&udpSockAddr, sizeof(udpSockAddr)) < 0){
            printf("sendto failed\n");
        }
        memset(bufData, 0, sizeof(bufData));
        vTaskDelay(3000 / portTICK_PERIOD_MS);
    }
#else
    // tcp服务端测试
    int port = 8000;

    int udpSock =  UDP_Init(NULL, &port, UDPSERMODE);
    if(udpSock < 0){
        ESP_LOGI(TAG[UDPSERMODE], "udp init failed");
        ESP_ERROR_CHECK(ESP_FAIL); // UDP初始化失败重启esp32
    }
    char bufData[128] = "";

    socklen_t udpSockCliAddrLen = sizeof(udpSockCliAddr);

    while(1){
        int err = recvfrom(udpSock, bufData, sizeof(bufData)-1, 0, (SA*)&udpSockCliAddr, &udpSockCliAddrLen);
        if(err < 0){
            ESP_LOGI("recvfrom", "failed, err:%d", err);
        }

        printf("recvfrom:%s\n", bufData);
        memset(bufData, 0, sizeof(bufData)-1);

        vTaskDelay(5000 / portTICK_PERIOD_MS);
    }
#endif
}
#endif

